import { queryContent } from "@/api/base";

class Dic {
    /**
     * 字典类型
     * @param {String} dicType 
     */
    constructor (dicType) {
        this.dicType = dicType;
        this.state = [
            { label: '1', value: '' }
        ];
        this.state = null;
    }

    init() {
        return queryContent(this.dicType).then((result) => {
            this.state = result.content.map(i => ({ label: i.dictLabel, value: i.dictValue }));
            return this.state;
        });
    }

    /**
     * 获取显示内容
     * @param {*} value 
     * @returns 
     */
    getLabel(value) {
        if (!this.state) {
            this.init();
        }
        const item = this.state.find(i => i.value == value);
        return item ? item.label : null;
    }

    /**
     * 获取选项
     * @param {Number} type 0.常规 1.带额外全部选项
     * @returns {Promise<{}[]>}
     */
    getOpts(type) {
        if (!this.state) {
            return this.init().then(() => this.getOpts(type));
        }
        const val = this.state.filter(i => i.label != '未知');
        if (type == 1) {
            val.unshift({ label: '全部', value: null });
        }
        return Promise.resolve(val);
    }
}
/**
 * 性别
 */
export const sexDic = new Dic('sys_user_sex');
/**
 * 专业
 */
export const majorDic = new Dic('edu_major');

/**
 * 年级
 */
export const gradeDic = new Dic('edu_grade');

/**
 * 政治面貌
 */
export const  policyIdentityDic = new Dic('edu_policy');
 
/**
 *  家庭成员类型
 */
export const  familyMemberTypeDic = new Dic('family_member_type');

/**
 *  试题类型
 */
// export const  examQuestionTypeDic = new Dic('exam_question_type');